<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;


use Symfony\Component\HttpFoundation\Request;

class BaseController4Back extends BaseController {

    public $data;
    public $theme = "Sano";

    public function show($template,$data = [],$response = null)
    {
//        $assetsStr = "/themes/sano";
//        $data["assetsRoot"] = $assetsStr;
//        $data["assetsRoot"] = $assetsStr;
//        $data["rnd"] = "?rnd=".time();
//        dd($request);
        $request = Request::createFromGlobals();
        $data["uri"] = $request->getPathInfo();
        $bc = new BaseCache();
        $menu = $bc->getItem("initMenu");
        $data["menu"] = $menu;
        $twigStr = "Back/".$this->theme."/".$template.".html.twig";
        $assetsStr = "/themes/sano";
        $data["assetsRoot"] = $assetsStr;
        $data["rnd"] = "?rnd=".time();
        return $this->render($twigStr,$data,$response);
    }

    public function needToBeLogin()
    {
        $url = $this->generateUrl("BackLogin");
        header("location:$url");
        die();
//
//        header('content-type:application/json');
//        header('charset:utf8');
//        header("HTTP/1.1 203 Non-Authoritative Information");
//        $s = 203;
//        $i = "正在进行身份验证";
//        $re = array(
//            "message"    => "请登录后再操作",
//            "data"    => [],
//        );
//        die(json_encode($re));
    }

    public function isAdmin()
    {
        $roles = $this->getSessionData("role");
        if (is_array($roles) && in_array("ROLE_ADMIN",$roles)) {
            return $this->getSessionData("id");
        } else {
            return false;
        }
    }

    public function needToBeAdmin()
    {
//        return 1;
        if ($this->isAdmin()) {
            return $this->getSessionData("id");
        } else {
            $this->needToBeLogin();
        }
    }

}
